package com.zmn.plat.admin.controller.customer;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.area.BaseArea;
import com.zmn.plat.model.entity.user.Customer;
import com.zmn.plat.model.entity.user.CustomerQuery;
import com.zmn.plat.model.entity.user.User;

/**
 * @author nowind
 * @since 18/04/19 11:58
 */
@Controller
@RequestMapping("/customer/")
public class CustomerListController extends CustomerBaseController {
    private Logger logger = LoggerFactory.getLogger(CustomerListController.class);

    // region 客户列表
    @RequestMapping(value = "list")
    public ModelAndView list(@ModelAttribute CustomerQuery query, @RequestParam(value = "userName", defaultValue = "") String userName) {

        if (NumberUtil.isNumber(query.getRealName()) && query.getRealName().length() < 10) {
            query.setCustomerId(Integer.parseInt(query.getRealName()));
            query.setRealName("");
        } else {
            if (!StringUtil.isBlank(query.getRealName())) {
                query.setCustomerId(0);
            }
        }
        if (NumberUtil.isNumber(userName) && userName.length() < 10) {
            query.setUserId(Integer.valueOf(userName));
        } else {
            query.setUserId(0);
        }

        query.setUpdateTimeBegin(DateUtil.getDateStartString(query.getUpdateTimeBegin()));
        query.setUpdateTimeEnd(DateUtil.getDateEndString(query.getUpdateTimeEnd()));
        query.setCreateTimeBegin(DateUtil.getDateStartString(query.getCreateTimeBegin()));
        query.setCreateTimeEnd(DateUtil.getDateEndString(query.getCreateTimeEnd()));

        List<Customer> items = customerService.listPageByQuery(query);

        ModelAndView mav = new ModelAndView("customer/list");

        mav.addObject("items", items);
        mav.addObject("vo", query);

        mav.addObject("provinces", areaService.listByParentId(GlobalConsts.TOP_ID));
        mav.addObject("cityes", query.getProvinceId() > 0 ? areaService.listByParentId(query.getProvinceId()) : new ArrayList<BaseArea>());
        mav.addObject("countyes", query.getCityId() > 0 ? areaService.listByParentId(query.getCityId()) : new ArrayList<BaseArea>());
        mav.addObject("streets", query.getCountyId() > 0 ? areaService.listByParentId(query.getCountyId()) : new ArrayList<BaseArea>());
        mav.addObject("statuses", GlobalDict.getStatusList(true));
        mav.addObject("genders", GlobalDict.getGenderList());

        return mav;
    }
    // endregion

    // region 客户详情
    @RequestMapping("detail")
    public ModelAndView detail(@RequestParam(value = "customerId", defaultValue = "0") int customerId) {

        Customer customer = customerId > 0 ? customerService.findByKey(customerId) : new Customer();
        User user = customer.getUserId() > 0 ? userService.findByKey(customer.getUserId()) : new User();

        StringBuilder orgaSb = new StringBuilder();

        StringBuilder areaSb = new StringBuilder();
        if (customer.getProvinceId() > GlobalConsts.TOP_ID) {
            areaSb.append(areaService.findByKey(customer.getProvinceId()).getName());
        }
        if (customer.getCityId() > GlobalConsts.TOP_ID) {
            areaSb.append(" ").append(areaService.findByKey(customer.getCityId()).getName());
        }
        if (customer.getCountyId() > GlobalConsts.TOP_ID) {
            areaSb.append(" ").append(areaService.findByKey(customer.getCountyId()).getName());
        }
        if (customer.getStreetId() > GlobalConsts.TOP_ID) {
            areaSb.append(" ").append(areaService.findByKey(customer.getStreetId()).getName());
        }

        ModelAndView mav = new ModelAndView("customer/detail");

        mav.addObject("customer", customer);
        mav.addObject("staffName", customer.getRealName());
        mav.addObject("customerOrga", orgaSb.toString());
        mav.addObject("customerArea", areaSb.toString());

        return mav;
    }
    // endregion

    // region 客户详情修改
    @RequestMapping("detailEdit")
    public ModelAndView detailEdit(@RequestParam(value = "customerId", defaultValue = "0") int customerId,
        @RequestParam(value = "userId", defaultValue = "0") int userId) {

        Customer customer = customerId > 0 ? customerService.findByKey(customerId) : new Customer();
        if (customerId == 0) {
            // 从用户创建
            if (userId > 0) {
                User user = userService.findByKey(userId);
                customer.setUserId(user.getUserId());
                customer.setRealName(user.getRealName());
                customer.setGender(user.getGender());
                customer.setMobile(user.getMobile());
                customer.setMail(user.getMail());
            }
        }

        ModelAndView mav = new ModelAndView("customer/detail_edit");

        mav.addObject("customer", customer);

        mav.addObject("provinces", areaService.listByParentId(GlobalConsts.TOP_ID));
        mav.addObject("cityes",
            customer.getProvinceId() > 0 ? areaService.listByParentId(customer.getProvinceId()) : new ArrayList<BaseArea>());
        mav.addObject("countyes", customer.getCityId() > 0 ? areaService.listByParentId(customer.getCityId()) : new ArrayList<BaseArea>());
        mav.addObject("streets",
            customer.getCountyId() > 0 ? areaService.listByParentId(customer.getCountyId()) : new ArrayList<BaseArea>());
        mav.addObject("statuses", GlobalDict.getStatusList(true));
        mav.addObject("genders", GlobalDict.getGenderList());

        return mav;
    }
    // endregion

}
